package com.eastseeway.radioproject.utils;

import com.eastseeway.radioproject.entities.BaseTCPBean;
import com.google.gson.Gson;

import java.nio.ByteOrder;

import struct.JavaStruct;
import struct.StructException;

public class StructTransUtil {
    public static <T> T unpack(T t,byte[] source) throws StructException {
        JavaStruct.unpack(t,source, ByteOrder.LITTLE_ENDIAN);
        byte[] pack = JavaStruct.pack(t, ByteOrder.LITTLE_ENDIAN);
        JavaStruct.unpack(t,pack, ByteOrder.LITTLE_ENDIAN);
        return t;
    }
    public static <T> byte[] pack(int address, T t) throws StructException{
        BaseTCPBean baseTCPBean = new BaseTCPBean();
        baseTCPBean.setAddress(address);
        baseTCPBean.setContent(JavaStruct.pack(t,ByteOrder.LITTLE_ENDIAN));
        return JavaStruct.pack(baseTCPBean,ByteOrder.LITTLE_ENDIAN);
    }

//    将要发送出去的命令转化成JavaBean类，从而可以进一步处理
    public static <T> T convertByteArrayToString(T t,byte[] source) throws StructException{
        return unpack(t, unpack(new BaseTCPBean(), source).getContent());
    }
}
